home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / window / ultrawin / uwshadow / shadow.c next >
Encoding:
C/C++ Source or Header  |  1992-03-05  |  10.0 KB  |  252 lines

  1. /****************************************************************************/
  2. /*                                                                                                                                                    */
  3. /* SHADOW.C                                                                                                                                 */
  4. /*                                                                                                                                                    */
  5. /* This program show how to use UltraWin with those pretty 3-d shadowed         */
  6. /* windows everyone likes to do.  Just take the routines you want below,    */
  7. /* and put them into your own programs!                                     */
  8. /*                                                                          */
  9. /*                                                         Boyd Gafford            */
  10. /*                                                         EnQue Software        */
  11. /*                                                         02/25/92         */
  12. /*                                                                                                                                                    */
  13. /****************************************************************************/
  14. #include <ctype.h>
  15. #include "uw.h"
  16. #include "uw_globx.h"
  17. #include "uw_keys.h"
  18.  
  19.  
  20. #define    SH_TOP_LEFT            0                                /* used by shadow window functions    */
  21. #define SH_TOP_RIGHT        1
  22. #define SH_BOTTOM_RIGHT    2
  23. #define    SH_BOTTOM_LEFT    3
  24.  
  25. #define    NUM_WINDOWS         15                            /* for the popup shadow demo                */
  26.  
  27. /*----------------------- global window variables --------------------------*/
  28. WINDOW    Desk_window;                         /* global window for "desktop"         */
  29. WINDOW    Test_window;                                        /* global POPUP window for demo            */
  30. WINDOW    Win_array[NUM_WINDOWS];
  31.  
  32. char        *Test_str =
  33. "Now is the time for all good men to come to the aid of their country.  ";
  34.  
  35. /*------------------------------ prototypes --------------------------------*/
  36. void shadow_window( WINDOW *wnp, int type, int w, int h, int att );
  37. void unshadow_window( WINDOW *wnp );
  38.  
  39.  
  40. /*********/
  41. /* ~main */
  42. /*       ********************************************************************/
  43. /* This is the main function, which does the init stuff and demos the new   */
  44. /* shadow routines.                                                         */
  45. /****************************************************************************/
  46. int main()
  47. {
  48.     WINDOW    *desk_wnp = &Desk_window;
  49.     WINDOW    *test_wnp = &Test_window;
  50.     int         i, x1, y1, x2, y2, width, height;
  51.  
  52.     /*---------------------------- init stuff --------------------------------*/
  53.     init_video(80, 25);                                                        /* try an 80x25 screen            */
  54.     init_clock(0x3333);                                                        /* init to 91 ticks/second    */
  55.     init_mouse();                                                                    /* init the mouse                        */
  56.     wn_create(0, 0, V_cols - 1, V_rows - 1,                /* one full-screen window        */
  57.                         NO_BDR, WN_POPUP, desk_wnp);
  58.     wn_color(BLACK, BLUE, desk_wnp);
  59.     wn_bdr_color(BLACK, BLUE, desk_wnp);
  60.     desk_wnp->scroll = OFF;                                                /* turn off desktop scroll    */
  61.     wn_set(desk_wnp);                                                            /* put the window on-screen    */
  62.     for (i=0; i<222; i++)                                                    /* do a little background        */
  63.     {                                                                                            /* to show off the shadow        */
  64.         wn_color((i % 8) + 8, BLUE, desk_wnp);
  65.         wn_st("UltraWin ", desk_wnp);
  66.     }
  67.     wn_color(BLACK, BLUE, desk_wnp);
  68.  
  69.  
  70.     /*---------------------- first, a simple demo ----------------------------*/
  71.     wn_create(7, 10, 72, 15,                                            /* create the demo window        */
  72.                         SGL_BDR, WN_POPUP, test_wnp);
  73.     wn_color(WHITE, LIGHTGRAY, test_wnp);
  74.     wn_bdr_color(BLACK, LIGHTGRAY, test_wnp);
  75.     wn_name(" Press any key ", test_wnp);
  76.  
  77.     wn_set(test_wnp);                                                            /* put the window on-screen    */
  78.     wn_plst(CENTERED, 2,
  79.         "Window without a shadow.", test_wnp);
  80.     wait_event();
  81.  
  82.     mv_cs(0, 2, test_wnp);                                                /* show the 4 shadow types    */
  83.     wn_cleol(test_wnp);
  84.     wn_plst(CENTERED, 2,
  85.         "Low shadowed window.", test_wnp);
  86.     for (i=SH_TOP_LEFT; i<=SH_BOTTOM_LEFT; i++)
  87.     {
  88.         shadow_window(test_wnp, i, 2, 1, DARKGRAY);
  89.         wait_event();
  90.         unshadow_window(test_wnp);
  91.     }
  92.  
  93.     mv_cs(0, 2, test_wnp);                                                /* show w and h variances        */
  94.     wn_cleol(test_wnp);
  95.     wn_plst(CENTERED, 2,
  96.         "High shadowed window.", test_wnp);
  97.     for (i=SH_TOP_LEFT; i<=SH_BOTTOM_LEFT; i++)
  98.     {
  99.         shadow_window(test_wnp, i, 4, 2, DARKGRAY);
  100.         wait_event();
  101.         unshadow_window(test_wnp);
  102.     }
  103.  
  104.     wn_destroy(test_wnp);                                                    /* destroy the window                */
  105.  
  106.     /*----------------------- now, lets go crazy! ----------------------------*/
  107.     randomize();
  108.     for ( i = 0; i < NUM_WINDOWS; i++ )
  109.     {
  110.         width = random(20) + 15;
  111.         height = random(3) + 4;
  112.         x1 = random(V_cols - width - 1);
  113.         y1 = random(V_rows - height - 1);
  114.         x2 = x1 + width - 1;
  115.         y2 = y1 + height - 1;
  116.         wn_create(x1, y1, x2, y2, SGL_BDR, WN_POPUP, &Win_array[i]);
  117.         wn_color((i % 8) + 8, i % 8, &Win_array[i]);
  118.         wn_bdr_color((i % 8) + 8, i % 8, &Win_array[i]);
  119.     }
  120.     for ( i = 0; i < NUM_WINDOWS; i++ )
  121.     {
  122.         wn_set(&Win_array[i]);
  123.         shadow_window(&Win_array[i], SH_BOTTOM_RIGHT, 2, 1, DARKGRAY);
  124.         wn_st(Test_str, &Win_array[i]);
  125.         wn_st(Test_str, &Win_array[i]);
  126.     }
  127.     wait_event();
  128.     for( i = NUM_WINDOWS - 1; i >= 0; i-- )
  129.     {
  130.         unshadow_window(&Win_array[i]);
  131.         wn_destroy(&Win_array[i]);
  132.     }
  133.  
  134.     /*------------------------- termination stuff ----------------------------*/
  135.     wn_destroy(desk_wnp);                                                    /* destroy the desk window    */
  136.     end_mouse();                                                                    /* terminate the mouse            */
  137.     end_video();                                                                    /* back to old video mode        */
  138.     return(1);                                                                        /* exit the progarm                    */
  139. }
  140. /*** end of main ***/
  141.  
  142. /******************/
  143. /* ~shadow_window */
  144. /*                ***********************************************************/
  145. /* This routine takes the window passed by pointer and shadows it.  It does */
  146. /* this by creating sub windows using the usr_ptr member of the window      */
  147. /* structure.                                                               */
  148. /*                                                                          */
  149. /* The contents under these sub windows are read off the screen and put         */
  150. /* inside the windows.  The attribute is then changed and the window is            */
  151. /* placed on the screen, giving the illusion of a shadow.                   */
  152. /*                                                                          */
  153. /* You may pass this routine a width, height and attribute (DARKGRAY on          */
  154. /* BLACK with a width of 2 and height of 1 usually looks nice).             */
  155. /* You may also pass a type, which is a number from 0-3 (see #defines) that */
  156. /* indicates the direction the shadow falls.                                */
  157. /*                                                                          */
  158. /* The main thing to remember is be SURE that you call the unshadow_window    */
  159. /* function BEFORE you destroy the window, or else you will have heap              */
  160. /* problems.                                                                                     */
  161. /****************************************************************************/
  162. void shadow_window( WINDOW *wnp, int type, int w, int h, int att )
  163. {
  164.     WINDOW     *wnp1, *wnp2;                                                    /* shadow window pointers        */
  165.     int            x1 = wnp->pane.x_min;                                    /* get the screen coords        */
  166.     int            y1 = wnp->pane.y_min;
  167.     int            x2 = wnp->pane.x_max;
  168.     int            y2 = wnp->pane.y_max;
  169.     int            win1_x1, win1_y1, win1_x2, win1_y2;
  170.     int            win2_x1, win2_y1, win2_x2, win2_y2;
  171.     int            i;
  172.     uchar        *dest;
  173.  
  174.     if (wnp->usr_ptr == NULL)
  175.     {
  176.         switch(type)
  177.         {
  178.             case SH_TOP_LEFT:
  179.                 win1_x1 = x1 - w, win1_y1 = y1 - h;            /* left side of window             */
  180.                 win1_x2 = x1 - 1, win1_y2 = y2 - h;
  181.                 win2_x1 = x1    , win2_y1 = y1 - h;            /* top side of window                */
  182.                 win2_x2 = x2 - w, win2_y2 = y1 - 1;
  183.                 break;
  184.             case SH_TOP_RIGHT:
  185.                 win1_x1 = x2 + 1, win1_y1 = y1 - h;            /* right side of window             */
  186.                 win1_x2 = x2 + w, win1_y2 = y2 - h;
  187.                 win2_x1 = x1 + w, win2_y1 = y1 - h;            /* top side of window                */
  188.                 win2_x2 = x2    , win2_y2 = y1 - 1;
  189.                 break;
  190.             case SH_BOTTOM_LEFT:
  191.                 win1_x1 = x1 - w, win1_y1 = y1 + h;            /* left side of window             */
  192.                 win1_x2 = x1 - 1, win1_y2 = y2 + h;
  193.                 win2_x1 = x1    , win2_y1 = y2 + 1;            /* bottom side of window        */
  194.                 win2_x2 = x2 - w, win2_y2 = y2 + h;
  195.                 break;
  196.             default:
  197.                 win1_x1 = x2 + 1, win1_y1 = y1 + h;            /* right side of window             */
  198.                 win1_x2 = x2 + w, win1_y2 = y2 + h;
  199.                 win2_x1 = x1 + w, win2_y1 = y2 + 1;            /* bottom side of window        */
  200.                 win2_x2 = x2    , win2_y2 = y2 + h;
  201.                 break;
  202.         }
  203.         wnp->usr_ptr = calloc(1, sizeof(WINDOW));        /* alloc/create first win        */
  204.         wn_create(win1_x1, win1_y1, win1_x2, win1_y2,
  205.                             NO_BDR, WN_POPUP,
  206.                             (WINDOW *) wnp->usr_ptr);
  207.         wnp1 = (WINDOW *) wnp->usr_ptr;
  208.         wn_read(wnp1);
  209.         dest = wnp1->buff + 1;                                            /* set to DARKGRAY on BLACK    */
  210.         for (i=0; i<(wnp1->cols * wnp1->rows); i++)
  211.             *dest = att, dest += 2;
  212.  
  213.         wnp1->usr_ptr = calloc(1, sizeof(WINDOW));    /* alloc/create second win    */
  214.         wn_create(win2_x1, win2_y1, win2_x2, win2_y2,
  215.                             NO_BDR, WN_POPUP,
  216.                             (WINDOW *) wnp1->usr_ptr);
  217.         wnp2 = (WINDOW *) wnp1->usr_ptr;
  218.         wn_read(wnp2);
  219.         dest = wnp2->buff + 1;                                            /* set to DARKGRAY on BLACK    */
  220.         for (i=0; i<(wnp2->cols * wnp2->rows); i++)
  221.             *dest = att, dest += 2;
  222.  
  223.         wn_save(wnp1), wn_rfsh(wnp1);                                /* do the wn_set equivalent    */
  224.         wn_save(wnp2), wn_rfsh(wnp2);
  225.     }
  226. }
  227. /*** end of shadow_window ***/
  228.  
  229. /********************/
  230. /* ~unshadow_window */
  231. /*                  *********************************************************/
  232. /*  This is the complement to shadow_window.  You MUST call this function   */
  233. /* before you destroy the window that has been shadowed or you will have        */
  234. /* severe problems.  If you don't it will have the same effect as not             */
  235. /* calling win_destroy on a created window.  Your computer will lockup!            */
  236. /****************************************************************************/
  237. void unshadow_window( WINDOW *wnp )
  238. {
  239.     WINDOW     *wnp1, *wnp2;                                                    /* shadow window pointers        */
  240.  
  241.     if (wnp->usr_ptr)                             /* does it have a shadow?        */
  242.     {
  243.         wnp1 = (WINDOW *) wnp->usr_ptr;                            /* get the window pointers    */
  244.         wnp2 = (WINDOW *) wnp1->usr_ptr;
  245.         wn_destroy(wnp2), wn_destroy(wnp1);
  246.         free(wnp2), free(wnp1);                                            /* and free the structure        */
  247.         wnp->usr_ptr = NULL;
  248.     }
  249. }
  250. /*** end of unshadow_window ***/
  251.  
  252. /*** END OF FILE ***/